package org.easy.miniooss;


import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({MinIOOssProperties.class})
@AutoConfigureAfter
@Slf4j
public class MinIOAutoConfiguration {
    private final MinIOOssProperties minIOOssProperties;

    public MinIOAutoConfiguration(MinIOOssProperties minIOOssProperties) {
        this.minIOOssProperties = minIOOssProperties;
        checkParam();
    }

    @Bean
    @ConditionalOnMissingBean
    public MinioOssTemplate minioOssTemplate() {
        MinioOssAdapter minioOssAdapter = new MinioOssAdapter();
        return new MinioOssTemplate(minIOOssProperties, minioOssAdapter);
    }

    @Bean
    @ConditionalOnMissingBean
    public MinioOssAdapter minioOssAdapter() {
        return new MinioOssAdapter();
    }


    private void checkParam() {
        if (minIOOssProperties.getEndpoint() == null ||
                minIOOssProperties.getAccessKeyId() == null ||
                minIOOssProperties.getAccessKeySecret() == null) {
            log.error("未配置" + MinIOAutoConfiguration.class.getTypeName());
        }

    }


}
